<?php

namespace App\Models\Product;

use Illuminate\Database\Eloquent\Model;
use App\Models\Region as RegionModel;//地区
use App\Models\Expressquery as ExpressqueryModel;//快递查询
use App\Models\Depot\Back as BackModel;

use DateTimeInterface;//时间格式化

class Order extends Model
{
    protected $table = 'order';

    protected $guarded = [];

    protected $dateFormat = 'U';

    const CREATED_AT = 'createtime';

    const UPDATED_AT = null;

    protected $appends = [
        'status_text', // 订单状态
        'region_text', //地区
        'express_text', //物流
        'back_text', // 退货表状态
    ];

    protected function serializeDate(DateTimeInterface $date)//序列化日期
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function getStatusTextAttribute()//订单状态
    {
        $status = $this->attributes['status'];
        switch ($status) {
            case '0':
                return '待支付';
            case '1':
                return '待发货';
            case '2':
                return '待收货';
            case '3':
                return '待评价';
            case '4':
                return '已完结';
            case '-1':
                return '仅退款';
            case '-2':
                return '退款退货';
            case '-3':
                return '待退货';
            case '-4':
                return '退款成功';
            case '-5':
                return '退款失败';
        }
    }

    public function getRegionTextAttribute()//获取地区
    {
        $region = '';

        $province = RegionModel::where('code', $this->attributes['province'])->value('name');

        if($province)
        {
            $region = $province;
        }

        $city = RegionModel::where('code', $this->attributes['city'])->value('name');

        if($city)
        {
            $region .= $city;
        }

        $district = RegionModel::where('code', $this->attributes['district'])->value('name');

        if($district)
        {
            $region .= $district;
        }

        return $region . $this->attributes['address'];
    }

    public function getExpressTextAttribute()//快递
    {
        $expressid = !empty($this->attributes['expressid']) ? $this->attributes['expressid'] : '0';

        $name = ExpressqueryModel::where('id', $expressid)->value('name');

        return $name;
    }

    public function getBackTextAttribute()
    {
        $status = false;

        $backinfo = BackModel::where('ordercode', $this->attributes['code'])->first();

        if($backinfo)
        {
            $status = true;
        }

        return $status;
    }

}